/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//expr_identity.hpp

#ifndef MVL_EXPR_IDENTITY_HPP
#define MVL_EXPR_IDENTITY_HPP

#include "layout.hpp"
#include "expr.hpp"

namespace mvl {

template<typename T, typename R, typename C, typename Order>
class expr_identity : public expr< expr_identity<T, R, C, Order> > 
{ 
public:
    typedef expr_identity self_t;

public:
    typedef T                  value_type;
    typedef Order              order;
    typedef R                  Rows;
    typedef C                  Cols;
    typedef meta::int_<0>      Size;

    typedef const self_t const_closure_t;
    typedef const_closure_t closure_t;

private:
    typedef typename layout::get1<Rows, Cols, order>::type size1;
    typedef typename meta::min<Rows, Cols>::type num_entries; 

    template<typename Data, typename I, typename Condition>
    struct state {
        typedef Data        data;
        typedef I           i;
        typedef Condition   condition;
        typedef state       type;
    };

    struct update {
        template<typename State>
        struct apply {
            typedef typename meta::next<typename State::i>::type new_i;
            typedef typename meta::eval_if<
                typename meta::less<typename State::i, num_entries>::type,
                typename meta::push_back<
                    meta::vector<>,
                    layout::entry<
                        typename State::i,
                        typename State::i,
                        layout::_1_
                    >
                >,
                meta::vector<>
            >::type new_element;
            typedef state<
                typename meta::push_back<
                    typename State::data,
                    new_element
                >,
                new_i,
                typename meta::not_equal_to<new_i, size1>::type
            > type;
        };
    };

    typedef typename meta::while_<
        state<
            meta::vector<>,
            meta::int_<0>,
            typename meta::not_equal_to<meta::int_<0>, size1>::type
        >,
        update
    >::type::data entries; 

public:
    typedef layout::sparse_layout<Rows::value, Cols::value, order, entries, 0> layout_t;

};

template<typename T, int R, int C>
struct identity_mat_traits 
{
    typedef const expr_identity<T, meta::int_<R>, meta::int_<C>, layout::row_major_tag> type;
};

template<typename T, int R, int C> 
inline 
typename identity_mat_traits<T, R, C>::type
identity_mat() {
    typedef typename identity_mat_traits<T, R, C>::type expr_t;
    return expr_t();
}

} // namespace mvl


#endif
